package org.nowcoder.leetcode.bit;

import java.util.ArrayList;
import java.util.List;

/**
 * Title  : 228. Summary Ranges
 * Source : https://leetcode.cn/problems/summary-ranges/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-08-07
 */
public class LeetCode_228 {
    public List<String> summaryRanges(int[] nums) {
        List<String> results = new ArrayList<>();
        //边界条件
        if (nums.length == 0) {
            return results;
        }
        if (nums.length == 1) {
            results.add(String.valueOf(nums[0]));
            return results;
        }
        int left = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i - 1] + 1 != nums[i]) {
                if (left == i - 1) {
                    results.add(String.valueOf(nums[left]));
                } else {
                    results.add(nums[left] + "->" + nums[i - 1]);
                }
                left = i;
            }
            if (i == nums.length - 1) {
                if (left == i) {
                    results.add(String.valueOf(nums[left]));
                } else {
                    results.add(nums[left] + "->" + nums[i]);
                }
            }
        }
        return results;
    }
}
